//
//  MainView.m
//
//  Created by Nikita Ivanyushchenko on 3/11/10.
//  Copyright 2010 MLS-Automatization. All rights reserved.
//

#import "MainView.h"
#import "ImageSliceOperation.h"
#import "iPictureSplitterAppDelegate.h"
#import "MyImageView.h"

@implementation MainView

-(id) initWithCoder: (NSCoder*) coder 
{	
	self = [super initWithCoder: coder];  	
	if (self)
	{
		queue = [[NSOperationQueue alloc] init];
		[queue setMaxConcurrentOperationCount:4];
	}
	return self;	
}

-(IBAction) startButtonPressed:(id)sender
{
	if (![imageView image])
	{
		NSAlert *alert = [[NSAlert alloc] init];
		[alert addButtonWithTitle:@"OK"];
		[alert setMessageText:@"Error!"];
		[alert setInformativeText:@"You must open file previously."];
		[alert setAlertStyle: NSCriticalAlertStyle];
		[alert runModal];
		[alert release];
		return;
	}
	NSOpenPanel *saveDlg = [NSOpenPanel openPanel];  //select destination
	[saveDlg setCanCreateDirectories:YES];
	[saveDlg setCanChooseDirectories: YES];
	[saveDlg setCanChooseFiles: NO];
	int result = [saveDlg runModal];
	if(result == NSOKButton)
	{
		NSURL * dirURL = [saveDlg directoryURL];
		NSArray * argArray = [NSArray arrayWithObjects:	dirURL,
														[imageView image],
														[NSNumber numberWithInt:[vPartsLabel intValue]],
														[NSNumber numberWithInt:[hPartsLabel intValue]], 
															nil];
		[NSThread detachNewThreadSelector:@selector(splitImage:) toTarget:self withObject:argArray];
	}
}

-(IBAction) hPartsSliderMoved:(id)sender
{
	int val = [sender intValue];
	[hPartsLabel setStringValue:[NSString stringWithFormat:@"%i",val]];
	imageView.hSize = val;
	[imageView setNeedsDisplay:YES];
}

-(IBAction) vPartsSliderMoved:(id)sender
{
	int val = [sender intValue];
	[vPartsLabel setStringValue:[NSString stringWithFormat:@"%i",val]];
	imageView.vSize = val;
	[imageView setNeedsDisplay:YES];
}

-(void) splitImage:(NSArray*) anArray //0 - destination URL; 1 - image; 2 - vParts; 3 - hParts
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	[startButton setEnabled:NO];
	[progressIndicator setUsesThreadedAnimation:YES];
	[progressIndicator startAnimation:self];
	
	NSURL* anUrl		= [anArray objectAtIndex:0];
	NSImage* anImage	= [anArray objectAtIndex:1];
	int vParts			= [[anArray objectAtIndex:2] intValue];
	int hParts			= [[anArray objectAtIndex:3] intValue];
		
	int imgW = [anImage size].width;
	int imgH = [anImage size].height;
		
	int partW = imgW/hParts;
	int partH = imgH/vParts;
	
	for (int i=0; i<hParts; i++)
	{
		for (int j=0; j<vParts; j++)
		{
			int currentX = partW*i;
			int currentY = imgH - partH*(j+1);
			NSRect rect = NSMakeRect(currentX, currentY, partW, partH);
			NSString * fileName = [NSString stringWithFormat:@"%i_%i.jpg",i,j];
			NSURL * fileURL = [NSURL URLWithString:fileName relativeToURL:anUrl];
			
			ImageSliceOperation * op = [[ImageSliceOperation alloc] initWithImage:anImage rect:rect url:fileURL];
			[queue addOperation:op];
			[op release];
		}
	}
	
	[queue waitUntilAllOperationsAreFinished];
	[progressIndicator stopAnimation:self];
	
	[startButton setEnabled:YES];
	
	[pool release];
}

@end
